<?PHP
/**
* @company Libertas Solutions Ltd
* @package com.solutions.libertas.cms
* @author Adrian Sweeney
* @file libertas.mirror.php
* @date 09 Oct 2002
*/
/**
* Language module 
*/
class languages extends module{
	/**
	*  Class Variables
	*/
	var $module_name="languages";
	var $module_name_label="Language Management";
	var $module_label="Language Management";
	var $module_grouping="";
	var $module_admin="0";
	var $module_debug=false;
	var $module_creation="05/02/2003";
	var $module_modify	 		= '$Date: 2005/02/18 18:44:52 $';
	var $module_version 			= '$Revision: 1.14 $';
	var $module_command="LANGUAGE_"; 		// all commands specifically for this module will start with this token
	
	var $module_admin_options = array();
	var $module_admin_user_access = array();
	
	/**
	*  Class Methods
	*/
	
	function command($user_command,$parameter_list=array()){
		/**
		* If debug is turned on then output the command sent and the parameter list too.
		*/
		if ($this->module_debug){
			$this->call_command("UTILS_DEBUG_COMMAND_PARAMETERS",array($this->module_name,$user_command,$parameter_list,__LINE__,"command"));
		}
		/**
		* This is the main function of the Module this function will call what ever function
		* you want to call.
		*/
		if (strpos($user_command,$this->module_command)===0){
			if ($user_command==$this->module_command."DEBUG_ON"){
				$this->module_debug=true;
			}
			if ($user_command==$this->module_command."DEBUG_OFF"){
				$this->module_debug=false;
			}
			if ($user_command==$this->module_command."GET_MODULE"){
				return $this->get_module_name();
			}
			if ($user_command==$this->module_command."GET_VERSION"){
				return $this->get_module_version();
			}
			if ($user_command==$this->module_command."GET_AUTHOR"){
				return $this->get_module_author();
			}
			if ($user_command==$this->module_command."GET_CREATION"){
				return $this->get_module_creation();
			}
			if ($user_command==$this->module_command."ACCESS_OPTIONS"){
				return $this->module_admin_options(0);
			}
			if ($user_command==$this->module_command."ACCESS_DISPLAY_OPTIONS"){
				return $this->module_admin_access_options(0);
			}
			if ($user_command==$this->module_command."MENU_DISPLAY_OPTIONS"){
				return $this->display_channels($parameter_list);
			}
			/*
				module specific commands
			*/
			if ($user_command==$this->module_command."UPDATE_DEBUG"){
				$this->debug_update($parameter_list);
				$user_command=$this->module_command."DEBUG_ADMIN";
			}
			if ($user_command==$this->module_command."DEBUG_ADMIN"){
				return $this->debug_admin($parameter_list);
			}
			if ($user_command==$this->module_command."UPDATE_SYSTEM_PREFS"){
				$this->system_admin_save($parameter_list);
				$user_command=$this->module_command."SYSTEM_ADMIN";
			}
			if ($user_command==$this->module_command."SYSTEM_ADMIN"){
				return $this->system_admin($parameter_list);
			}
			if ($user_command==$this->module_command."SPLASH"){
				return $this->splash($parameter_list);
			}
			if ($user_command==$this->module_command."EXTRACT_SYSTEM_PREFERENCE"){
				return $this->extract_system_preference($parameter_list);
			}
			if ($user_command==$this->module_command."GET_COUNTRIES"){
				return $this->get_countries($parameter_list);
			}
		}else{
			return ""; // wrong command sent to system
		}
	}
	/**
	* call the initialisation function only when this module is created
	*/
	
	function initialise(){
		/**
		* request the client identifier once we use this variable often
		*/
		$this->client_identifier = $this->parent->client_identifier;
		/**
		* define some access functionality
		*/
		$this->module_admin_options			= array();
		$this->module_admin_user_access		= array();
	}


	
	
	
	function get_countries($parameters){
		$selected  			= $this->check_parameters($parameters,"selected");
		$restrict_country	= $this->check_parameters($parameters,"restrict_country",0);
		$as					= $this->check_parameters($parameters,"as","__XML__");
		if($restrict_country==0){
			$sql = "select cl_country, cl_identifier, cal_label from country_lookup 
						inner join country_area_lookup on cl_area =cal_code
					order by cal_label, cl_country";
		} else {
			$sql		= "select * from shop_weight_matrix where swm_client=$this->client_identifier";
			//print "<li>".__FILE__."@".__LINE__."<p>$sql</p></li>";
			$regions	= "";
			$countries	= "";
			$result 	= $this->db->query($sql);
            while($r 	= $this->db->fetch_array($result)){
				if($r["swm_country"]==-1){
					if($regions != ""){
						$regions .= ", ";
					}
					$regions .= "'".$r["swm_region"]."'";
				}
				if($r["swm_country"]!=-1){
					if($countries != ""){
						$countries .= ", ";
					}
					$countries .= $r["swm_country"];
				}
            }
			$where = "";
			if($regions!=""){
				$where .= " cl_area in ($regions)";
			}
			if($countries!=""){
				if($where != ""){
					$where .= " or ";
				}
				$where .= " cl_identifier in ($countries)";
			}
			if ($where !=""){
				$where = "where ($where)";
			}
            $this->db->free_result($result);
			$sql = "select distinct cl_country, cl_identifier, cal_label from country_lookup 
						inner join country_area_lookup on cl_area =cal_code
					$where 
					order by cal_label, cl_country";
//						inner join shop_weight_matrix on swm_country = cl_identifier and swm_client= $this->client_identifier
			//print "<li>".__FILE__."@".__LINE__."<p>$sql</p></li>";
		}
        $result  = $this->db->query($sql);
		$section ="";
		if($as=="__XML__"){
			$out="";
			$out .= "<optgroup label='Select One'>";
			$out .= "<option value=''>Select One</option>";
			$out .= "</optgroup>";
			$c=0;
	        while($r = $this->db->fetch_array($result)){
				$c++;
				$this_section = $r["cal_label"];
	        	if ($this_section != $section){
					if($section!=""){
						$out .= "</optgroup>";
					}
					$section = $this_section;
					$out .= "<optgroup label='".$r["cal_label"]."'>";
				}
				$out .= "<option value='".$r["cl_identifier"]."'";
				$cval = $r["cl_identifier"];
				$ctxt = $r["cl_country"];
				if($selected==$r["cl_identifier"]){
					$out .= " selected='true'";
				}
				$out .= "><![CDATA[".$r["cl_country"]."]]></option>";
	        }
			if($out !=""){
				$out .= "</optgroup>";
			}
	        $this->db->free_result($result);
			if($c==1){
				return Array("hide",$cval,$ctxt);
			}
		} else {
			$out=Array();
			$result  = $this->db->query($sql);
			$i=0;
		    while($r = $this->db->fetch_array($result)){
				$selectedValue="false";
				if($selected==$r["cl_identifier"]){
					$selectedValue="true";
				}
	        	$out[$i] = Array("label"=>$r["cl_country"], "value"=>$r["cl_identifier"],"section"=>$r["cal_label"],"selected"=>$selectedValue);
				$i++;
	        }
	        $this->db->free_result($result);
		}
		return $out;
	}
	function get_country($parameters){
		$selected  = $this->check_parameters($parameters,"selected");
	}
}
?>